﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraLaiNhaCungCap
{
    public partial class frmThanhToan : Form
    {
        #region Khởi tạo
        public static bool kiemtra = false;
        bool checkClose = false;
        Entities.TraLaiNCC temp;

        public static Entities.TraLaiNCC tralaincc;
        double tienboithuong = 0;

        public frmThanhToan(Entities.TraLaiNCC tralaincc)
        {
            InitializeComponent();
            try
            {
                txtThanhToanNgay.Focus();
                temp = tralaincc;
                lbGiaTriNoHienThoi.Text = Common.Utilities.FormatMoney(temp.NoHienThoi);
                lbGiaTriTongTien.Text = Common.Utilities.FormatMoney(temp.TienBoiThuong);
                txtThanhToanNgay.Text = temp.ThanhToanNgay;
                lbGiaTriThanhToanNgay.Text = Common.Utilities.FormatMoney(temp.ThanhToanNgay);
                tienboithuong = double.Parse(temp.NoHienThoi) - double.Parse(temp.TienBoiThuong);
                lbGiaTriDuTra.Text = Common.Utilities.FormatMoney((tienboithuong + double.Parse(lbGiaTriThanhToanNgay.Text)).ToString());
            }
            catch
            {
            }
        }
        #endregion
        
        #region thanh toán
        void ThanhToan()
        {
            try
            {
                try
                {
                    if (tralaincc.HanhDong == Common.Constants.Update)
                        kiemtra = true;
                }
                catch
                {
                }
                temp.ThanhToanNgay = lbGiaTriThanhToanNgay.Text;
                tralaincc = temp;
                checkClose = true;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Event
        private void frmThanhToan_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmThanhToan_FormClosing);
            txtThanhToanNgay.TextChanged += new EventHandler(txtThanhToanNgay_TextChanged);
            txtThanhToanNgay.KeyDown += new KeyEventHandler(txtThanhToanNgay_KeyDown);
        }

        void txtThanhToanNgay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                ThanhToan();
            }
        }

        void txtThanhToanNgay_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double i = double.Parse(txtThanhToanNgay.Text);
                if (i <= 0)
                {
                    txtThanhToanNgay.Text = "";
                    lbGiaTriThanhToanNgay.Text = "0";
                }
                lbGiaTriThanhToanNgay.Text = Common.Utilities.FormatMoney(txtThanhToanNgay.Text);
                lbGiaTriDuTra.Text = Common.Utilities.FormatMoney((tienboithuong + double.Parse(lbGiaTriThanhToanNgay.Text)).ToString());
            }
            catch
            {
                txtThanhToanNgay.Text = "";
                lbGiaTriThanhToanNgay.Text = "0";
                lbGiaTriDuTra.Text = (tienboithuong + double.Parse(lbGiaTriThanhToanNgay.Text)).ToString();
            }
        }

        void frmThanhToan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    kiemtra = true;
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion